const baseUrl1 = '/api'
export const post = (url, data = {}) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'post',
			header: {
				'content-type': 'application/json'
			},
			url: baseUrl1 + url,
			data,
			success: function(res) {
				const {
					data
				} = res
				resolve(data)
			},
			fail: function(err) {
				reject(err)
			}
		})
	})
}
// application/x-www-form-urlencoded

export const get = (url, data = {}) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'get',
			url: baseUrl1 + url,
			data,
			success: function(res) {
				resolve(res)
			},
			fail: function(err) {
				reject(err)
			}
		})
	})
}

//上传图片
export const uploadFile = (url, tempFilePath, data={})=> {
	return new Promise((resolve,reject)=>{
		const uploadTask = uni.uploadFile({
			url: baseUrl1 + url,
			filePath: tempFilePath,
			name: 'file',
			formData: data,
			success: (uploadFileRes) => {
				const {data} = uploadFileRes
				resolve(JSON.parse(data))
			},
			fail: (err) => {
				reject(err)
			}
		});
		
		// uploadTask.onProgressUpdate((res) => {
		// 	console.log('上传进度' + res.progress);
		// 	console.log('已经上传的数据长度' + res.totalBytesSent);
		// 	console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
		
		// 	// 测试条件，取消上传任务。
		// 	// if (res.progress > 50) {
		// 	//     uploadTask.abort();
		// 	// }
		// });
	})
	
}



const baseUrl2 = 'ws://39.100.203.75:8996/'
export const ws = (url, data = '') => {
	console.log('url',data)
	return uni.connectSocket({
		url: baseUrl2 + url + data,
		//    data(){
		// 	return data
		// },
		header: {
			// 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
			'content-type': 'application/json'
		},
		method: 'GET',
		complete: () => {}
	});
}
